package mortal.game.control
{
    import Framework.MQ.*;
    import Message.Command.*;
    import Message.Game.*;
    import Message.Public.*;
    import modules.*;
    import mortal.component.window.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.info.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class RankingController extends Controller
    {
        private var _ranking:Ranking2Module;
        private var _rankingType:int;
        private var _rankingCamp:int = 0;
        private var _rankingCareer:int = -1;
        private var _rankingSex:int = -1;
        private var _playerName:String;
        private var _isSearch:Boolean = false;
        private var _isLookUp:Boolean = false;
        private var _fromPlayerId:SEntityId;
        private var _serverId:int;
        private var _guildName:String;

        public function RankingController()
        {
            
        }

        override protected function initView() : IView
        {
            this._ranking = new Ranking2Module();
            this._fromPlayerId = cache.role.entityInfo.entityId;
            this._playerName = cache.role.playerInfo.name;
            if (cache.guild.myGuildInfo == null)
            {
                this._guildName = "";
            }
            else
            {
                this._guildName = cache.guild.myGuildInfo.guildName;
            }
            this._ranking.addEventListener(WindowEvent.SHOW, this.onWindowShowHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicToplistUpdate, this.onToplistUpdateHandler);
            return this._ranking;
        }

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.RankingTypeChange, this.onRankTypeChangeHandler);
            Dispatcher.addEventListener(EventName.RankingPageChange, this.onPageChangeHandler);
            Dispatcher.addEventListener(EventName.RankingCampChange, this.onRankCampChangeHandler);
            Dispatcher.addEventListener(EventName.RankingCareerChange, this.onRankCareerChangeHandler);
            Dispatcher.addEventListener(EventName.RankingSexChange, this.onRankSexChangeHandler);
            Dispatcher.addEventListener(EventName.RankingQueryByName, this.onQueryByNameHandler);
            Dispatcher.addEventListener(EventName.LookUpTargetEquip, this.onLookUpEquipHandler);
            Dispatcher.addEventListener(EventName.VotePlayer, this.onVotePlayerHandler);
            Dispatcher.addEventListener(EventName.GuildSearchSuccess, this.onGuildSearchSuccessHandler);
            Dispatcher.addEventListener(EventName.RankingGetMyRank, this.onRankingGetMyRankHandler);
            Dispatcher.addEventListener(EventName.RankGetVoteCountSuccess, this.onGetVoteCountSuccessHandler);
            NetDispatcher.addCmdListener(ServerCommand.GetRankingDataSuccess, this.onGetDataSuccessHandler);
            NetDispatcher.addCmdListener(ServerCommand.SearchPlayerResult, this.onSearchPlayer);
            NetDispatcher.addCmdListener(ServerCommand.SearchPlayerNoPlayer, this.onSearchPlayerNoHandler);
            NetDispatcher.addCmdListener(ServerCommand.PlayerInfoReply, this.onPlayerInfoMsg);
            NetDispatcher.addCmdListener(ServerCommand.PlayerNotOnline, this.onPlayerNotOnlineHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicVotePlayerUpdate, this.onVotePlayerUpdateHandler);
            
        }

        private function onRankTypeChangeHandler(event:DataEvent) : void
        {
            this._rankingType = event.data as int;
            if (this._ranking != null)
            {
                this._ranking.resetEquipData();
            }
            this.getCurRankList();
            
        }

        private function onWindowShowHandler(event:WindowEvent) : void
        {
            this.getCurRankList();
            if (cache.ranking.voteCount == -1)
            {
                GameProxy.rankingProxy.getRestVoteCount();
            }
            
        }

        private function getCurRankList() : void
        {
            var _loc_1:Array = null;
            var _loc_2:int = 0;
            cache.ranking.rankingType = this._rankingType;
            if (cache.ranking.isHaveList(this._rankingType) && !cache.ranking.getIsUpdate(this._rankingType))
            {
                _loc_1 = cache.ranking.getDataByPage(this._rankingType, 1);
                _loc_2 = cache.ranking.isSelfInRanking(this._rankingType, this._playerName, this._guildName);
                if (this._ranking != null)
                {
                    this._ranking.setData(_loc_1, this._rankingType);
                    this._ranking.setMaxPage(cache.ranking.curMaxPage);
                }
            }
            else
            {
                GameProxy.rankingProxy.getRankingData(this._rankingType);
            }
            
        }

        private function onPageChangeHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            var _loc_3:* = cache.ranking.getDataByPage(this._rankingType, _loc_2, this._rankingCamp, this._rankingCareer, this._rankingSex);
            if (this._ranking != null)
            {
                this._ranking.setData(_loc_3, this._rankingType, _loc_2);
            }
            
        }

        private function onRankCampChangeHandler(event:DataEvent) : void
        {
            this._rankingCamp = event.data as int;
            var _loc_2:* = cache.ranking.getDataByPage(this._rankingType, 1, this._rankingCamp, this._rankingCareer, this._rankingSex);
            if (this._ranking != null)
            {
                this._ranking.setData(_loc_2, this._rankingType, 1);
                this._ranking.setMaxPage(cache.ranking.curMaxPage);
            }
            
        }

        private function onRankCareerChangeHandler(event:DataEvent) : void
        {
            this._rankingCareer = event.data as int;
            var _loc_2:* = cache.ranking.getDataByPage(this._rankingType, 1, this._rankingCamp, this._rankingCareer, this._rankingSex);
            if (this._ranking != null)
            {
                this._ranking.setData(_loc_2, this._rankingType, 1);
                this._ranking.setMaxPage(cache.ranking.curMaxPage);
            }
            
        }

        private function onRankSexChangeHandler(event:DataEvent) : void
        {
            this._rankingSex = event.data as int;
            var _loc_2:* = cache.ranking.getDataByPage(this._rankingType, 1, this._rankingCamp, this._rankingCareer, this._rankingSex);
            if (this._ranking != null)
            {
                this._ranking.setData(_loc_2, this._rankingType, 1);
                this._ranking.setMaxPage(cache.ranking.curMaxPage);
            }
            
        }

        private function onQueryByNameHandler(event:DataEvent) : void
        {
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:Array = null;
            var _loc_7:int = 0;
            var _loc_2:* = event.data as String;
            var _loc_3:* = cache.ranking.isInRankingByName(this._rankingType, _loc_2);
            if (_loc_3 != 0)
            {
                _loc_4 = _loc_3 % 10;
                if (_loc_4 != 0)
                {
                    _loc_5 = _loc_3 / 10 + 1;
                }
                else
                {
                    _loc_5 = _loc_3 / 10;
                }
                _loc_6 = cache.ranking.getDataByPage(this._rankingType, _loc_5);
                if (this._ranking != null)
                {
                    this._ranking.setData(_loc_6, this._rankingType, _loc_5, false);
                    this._ranking.showTargetPalyer(this._rankingType, _loc_3);
                }
            }
            else
            {
                _loc_7 = this._rankingType / 100;
                if (_loc_7 != 2)
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.FriendSearchPlayer, _loc_2));
                }
                else
                {
                    GameProxy.guildProxy.searchGuilds(0, _loc_2);
                }
                this._isSearch = true;
            }
            
        }

        private function onLookUpEquipHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SEntityId;
            this.lookUpEquip(_loc_2);
            
        }

        private function lookUpEquip(param1:SEntityId) : void
        {
            this._isLookUp = true;
            GameProxy.playerInfoProxy.lookupRequest_async(this._fromPlayerId, param1);
            
        }

        private function onVotePlayerHandler(event:DataEvent) : void
        {
            var _loc_2:* = (event.data as Object).type;
            var _loc_3:* = (event.data as Object).playerId;
            GameProxy.rankingProxy.votePlayer(_loc_2, 0, _loc_3);
            
        }

        private function onGetDataSuccessHandler(param1:Object) : void
        {
            var _loc_3:Array = null;
            var _loc_4:int = 0;
            var _loc_2:* = param1.rankingList as Array;
            cache.ranking.setData(_loc_2, this._rankingType);
            cache.ranking.severdId = param1.serverId;
            this._serverId = param1.serverId;
            if (this._ranking != null)
            {
                _loc_3 = cache.ranking.getDataByPage(this._rankingType, 1);
                this._ranking.setData(_loc_3, this._rankingType);
                this._ranking.setMaxPage(cache.ranking.curMaxPage);
                _loc_4 = cache.ranking.isSelfInRanking(this._rankingType, this._playerName, this._guildName);
            }
            cache.ranking.setIsUpdateByType(this._rankingType, false);
            
        }

        private function onSearchPlayer(param1:Object) : void
        {
            var _loc_2:Boolean = false;
            if (this._isSearch)
            {
                _loc_2 = param1["isFind"];
                if (_loc_2)
                {
                    MsgManager.addBroadCast("该玩家暂时没有排名");
                }
                else
                {
                    MsgManager.addBroadCast("没有这个玩家");
                }
                this._isSearch = false;
            }
            
        }

        private function onSearchPlayerNoHandler(param1:Object) : void
        {
            if (this._isSearch)
            {
                MsgManager.addBroadCast("没有这个玩家");
                this._isSearch = false;
            }
            
        }

        private function onGuildSearchSuccessHandler(event:DataEvent) : void
        {
            var _loc_2:Array = null;
            if (this._isSearch)
            {
                _loc_2 = event.data as Array;
                if (_loc_2.length > 0)
                {
                    MsgManager.addBroadCast("该仙盟暂时没有排名");
                }
                else
                {
                    MsgManager.addBroadCast("没有这个仙盟");
                }
                this._isSearch = false;
            }
            
        }

        private function onRankingGetMyRankHandler(event:DataEvent) : void
        {
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:Array = null;
            var _loc_2:* = cache.ranking.isSelfInRanking(this._rankingType, this._playerName, this._guildName);
            if (_loc_2 == 0)
            {
                MsgManager.addBroadCast("你当前没有排名喔~");
            }
            else
            {
                _loc_3 = _loc_2 % 10;
                if (_loc_3 != 0)
                {
                    _loc_4 = _loc_2 / 10 + 1;
                }
                else
                {
                    _loc_4 = _loc_2 / 10;
                }
                _loc_5 = cache.ranking.getDataByPage(this._rankingType, _loc_4);
                if (this._ranking != null)
                {
                    this._ranking.setData(_loc_5, this._rankingType, _loc_4, false);
                    this._ranking.showTargetPalyer(this._rankingType, _loc_2);
                }
            }
            
        }

        private function onPlayerInfoMsg(param1:Object) : void
        {
            var _loc_2:SLookupPlayerReply = null;
            var _loc_3:int = 0;
            var _loc_4:Array = null;
            var _loc_5:int = 0;
            var _loc_6:Array = null;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            var _loc_9:int = 0;
            var _loc_10:Object = null;
            var _loc_11:SPlayerItemUpdate = null;
            var _loc_12:ItemData = null;
            if (this._isLookUp)
            {
                this._isLookUp = false;
                _loc_2 = param1 as SLookupPlayerReply;
                _loc_3 = _loc_2.miniPlayer.entityId.id;
                _loc_4 = _loc_2.playerItemUpdates;
                _loc_5 = _loc_4.length;
                _loc_6 = [];
                _loc_7 = 0;
                _loc_7 = 0;
                while (_loc_7 < 14)
                {
                    
                    _loc_6.push(null);
                    _loc_7++;
                }
                _loc_7 = 0;
                while (_loc_7 < _loc_5)
                {
                    
                    _loc_11 = _loc_4[_loc_7] as SPlayerItemUpdate;
                    if (_loc_11.playerItem)
                    {
                        _loc_12 = new ItemData(_loc_11.playerItem);
                        _loc_6[(_loc_11.posIndex - 1)] = _loc_12;
                    }
                    _loc_7++;
                }
                _loc_8 = _loc_2.beAdored;
                _loc_9 = _loc_2.beDespised;
                _loc_10 = {equipArr:_loc_6, adoreNum:_loc_8, despiseNum:_loc_9};
                if (this._ranking != null)
                {
                    this._ranking.setEquipData(_loc_10, _loc_3, _loc_2.miniPlayer);
                }
            }
            
        }

        private function onPlayerNotOnlineHandler(param1:Object) : void
        {
            var _loc_2:int = 0;
            if (this._isLookUp)
            {
                this._isLookUp = false;
                _loc_2 = param1 as int;
                if (this._ranking != null)
                {
                    this._ranking.setNotOnline(_loc_2);
                }
            }
            
        }

        private function onToplistUpdateHandler(param1:MessageBlock) : void
        {
            cache.ranking.setIsUpdate();
            if (this._ranking != null && !this._ranking.isHide)
            {
                GameProxy.rankingProxy.getRankingData(this._rankingType);
            }
            
        }

        private function onVotePlayerUpdateHandler(param1:MessageBlock) : void
        {
            var _loc_2:* = param1.messageBase as SVotePlayer;
            Dispatcher.dispatchEvent(new DataEvent(EventName.VotePlayerSuccess, _loc_2));
            
        }

        private function onGetVoteCountSuccessHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            cache.ranking.voteCount = _loc_2;
            if (this._ranking != null)
            {
                this._ranking.showVoteCount();
            }
            
        }

    }
}
